/**
 * ===========================================================================
 *
 *       Filename:  public.h
 *
 *    Description:  learn more
 *
 *        Version:  1.0
 *        Created:  04/14/09 22:22:46
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Author (mn), Email@xxx.xxx
 *        Company:  xxx xxx
 *
 * ===========================================================================
 */

#ifndef __PUBLIC_H__
#define __PUBLIC_H__

#include "protocol.h"
#include "logout.h"

#define DEF_BUFMAX  	8192
#define DEF_IPLEN	 	16
#define DEF_USERMAX		1024
#define DEF_GROUPMAX 	128
#define DEF_G_U_MAX	 	128

struct global_session_t
{
	char ip[DEF_IPLEN];
	short port;
	int sockfd;
} global_session;

struct user_session_t
{
	int conn;
	char recvbuf[DEF_BUFMAX];
	int recvlen;
	char sendbuf[DEF_BUFMAX];
	int sendlen;
	struct user_info_t user_info;
} g_user_list[DEF_USERMAX];

struct group_session_t
{
	struct group_info_t group_info;
	int user_cnt;
	struct user_session_t *user_list[DEF_G_U_MAX];
} g_group_list[DEF_GROUPMAX];

int g_group_cnt;
int g_user_cnt;

extern unsigned long get_rand_num();
extern int write_data(struct user_session_t *session, char *data, int len);
extern int pack_write(struct user_session_t *session, int type, void *data, int len);

extern struct user_session_t * get_usersession_by_id(unsigned long id);
extern struct group_session_t * get_groupsession_by_id(unsigned long id);

extern int add_user_session(int conn);
extern int del_user_session(int conn);
extern int add_group_session(struct group_info_t *group_info);
extern int del_group_session(unsigned long group_id);
extern int add_user_to_group(unsigned long group_id, struct user_session_t *session);
extern int del_user_from_group(unsigned long group_id, unsigned long user_id);
extern int del_user_from_all_group(unsigned long user_id);

#endif // __PUBLIC_H__
